/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utility;

import CRUD.MemberHome;
import CRUD.ThreadHome;
import CRUD.ThreadpollschoicesHome;
import CRUD.ThreadpollsquestionsHome;
import java.sql.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import xmlMapping.Threadpollschoices;
import xmlMapping.Threadpollsquestions;
import xmlMapping.Thread;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import xmlMapping.Member;
import xmlMapping.Message;

/**
 *
 * @author ABDELMOHSEN
 */
public class ThreadPollsUtility {
    Date sqlDate = new Date(new java.util.Date().getTime());
    public void addThreadPolls(Threadpollsquestions thPQ, Vector<Threadpollschoices> choices,Member operator){
        ThreadpollsquestionsHome thPQHome=new ThreadpollsquestionsHome();
        thPQHome.persist(thPQ);
        ThreadpollschoicesHome thPCHome=new ThreadpollschoicesHome();
        for(int i=0;i<choices.size();i++){
            thPCHome.persist(choices.get(i));
        }
        ThreadHome thHome=new ThreadHome();
        Thread th=thHome.findById(thPQ.getThread().getThreadId());
        Iterator<Member> markers=th.getMembers().iterator();
        MessageUtility mUtility=new MessageUtility();
        Message m=new Message(null, operator, MessageUtility.POLL_ADDED+th.getTitle(), MessageUtility.POLL_ADDED_TO_THREAD_MARKER + th.getTitle(), false, sqlDate);
        while(markers.hasNext()){
            m.setMemberByMemberId(markers.next());
            mUtility.sendMessage(m);
        }
    }
    /*public void editThreadPollsQuestion(Threadpollsquestions thPQ,Member operator){
                Session session=SessionFactoryUtil.getSessionFactory().getCurrentSession();
                session.beginTransaction();
		Query qur= session.createQuery("from  Threadpollsquestions where pollsId=:id");
		Iterator it=qur.setInteger("id", thPQ.getPollsId()).list().iterator();
                ThreadpollsquestionsHome qHome=new ThreadpollsquestionsHome();
                Threadpollsquestions qeust=qHome.findById(thPQ.getPollsId());
                qeust.setQuestion(thPQ.getQuestion());
                qeust.setThread(thPQ.getThread());
                session.getTransaction().commit();

                Query q= session.createQuery("from  Thread where threadId=:id");
                Iterator ite=q.setInteger("id", thPQ.getThread().getThreadId()).list().iterator();
                Thread th=(Thread)ite.next();
                Iterator<Member> markers=th.getMembers().iterator();
                MessageUtility mUtility=new MessageUtility();
                Message m=new Message(null, operator, MessageUtility.POLL_ADDED+th.getTitle(), MessageUtility.POLL_ADDED_TO_THREAD_MARKER + th.getTitle(), false, new Date());
                while(markers.hasNext()){
                    m.setMemberByMemberId(markers.next());
                    mUtility.sendMessage(m);
                }
    }*/
   /* public void editThreadPollsQuestionsAndChoices(Threadpollsquestions thPQ, Vector<Threadpollschoices> choices,Member operator){
                Session session=SessionFactoryUtil.getSessionFactory().getCurrentSession();
                session.beginTransaction();

                Query qur= session.createQuery("from  Threadpollsquestions where pollsId=:id");
		Iterator it=qur.setInteger("id", thPQ.getPollsId()).list().iterator();
                Threadpollsquestions qeust=(Threadpollsquestions)it.next();
                qeust.setQuestion(thPQ.getQuestion());
                qeust.setThread(thPQ.getThread());

                for(int i=0;i<choices.size();i++){
                    Threadpollschoices ch=choices.get(i);
                    Query q= session.createQuery("from  Threadpollschoices where choiceId=:id");
                    Iterator iterator=q.setInteger("id", ch.getChoiceId()).list().iterator();
                    Threadpollschoices choice=(Threadpollschoices)iterator.next();
                    choice.setChoice(ch.getChoice());
                    choice.setNoofselectors(ch.getNoofselectors());
                }

                session.getTransaction().commit();

                Query q= session.createQuery("from  Thread where threadId=:id");
                Iterator ite=q.setInteger("id", thPQ.getThread().getThreadId()).list().iterator();
                Thread th=(Thread)ite.next();
                Iterator<Member> markers=th.getMembers().iterator();
                MessageUtility mUtility=new MessageUtility();
                Message m=new Message(null, operator, MessageUtility.POLL_ADDED+th.getTitle(), MessageUtility.POLL_ADDED_TO_THREAD_MARKER + th.getTitle(), false, new Date());
                while(markers.hasNext()){
                    m.setMemberByMemberId(markers.next());
                    mUtility.sendMessage(m);
                }

    }*/
    public void deleteThreadPolls(int pollsID,Member operator){
                ThreadpollsquestionsHome qHome=new ThreadpollsquestionsHome();
                Threadpollsquestions qeust=qHome.findById(pollsID);
                ThreadHome thHome=new ThreadHome();
                Thread th=thHome.findById(qeust.getThread().getThreadId());
                Iterator<Member> markers=th.getMembers().iterator();
                MessageUtility mUtility=new MessageUtility();
                Message m=new Message(null, operator,MessageUtility.POLL_ADDED+th.getTitle(), MessageUtility.POLL_ADDED_TO_THREAD_MARKER + th.getTitle(), false, sqlDate);
                while(markers.hasNext()){
                    m.setMemberByMemberId(markers.next());
                    mUtility.sendMessage(m);
                }
                qHome.delete(qeust);
    }
    public void vote(int ch,int m){
                    ThreadpollschoicesHome home=new ThreadpollschoicesHome();
                    Threadpollschoices choice=home.findById(ch);
                    choice.setNoofselectors(choice.getNoofselectors()+1);
                    home.persist(choice);
                    MemberHome mHome=new MemberHome();
                    Member mem=mHome.findById(m);
                    Set<Threadpollsquestions> quests=mem.getThreadpollsquestionses();
                    ThreadpollsquestionsHome home2=new ThreadpollsquestionsHome();
                    Threadpollsquestions q=choice.getThreadpollsquestions();
                    quests.add(q);
                    mem.setThreadpollsquestionses(quests);
                    mHome.persist(mem);
    }
    public Vector<Threadpollschoices> getThreadPollsChoices(int ThreadpollsquestionsID){
        Vector<Threadpollschoices> v=null;
        Threadpollschoices qeust=null;
        ThreadpollsquestionsHome qHome=new ThreadpollsquestionsHome();
        Threadpollsquestions qeust2=qHome.findById(ThreadpollsquestionsID);
        Iterator it=qeust2.getThreadpollschoiceses().iterator();
        while(it.hasNext()){
            qeust=(Threadpollschoices)it.next();
            if(v==null) v=new Vector<Threadpollschoices>();
            v.add(qeust);
        }
        return v;
    }
    public Threadpollsquestions getThreadPollsQuestions(int Threadid){
        Session session=SessionFactoryUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Vector<Threadpollschoices> v=null;
        Query qur2= session.createQuery("from  Threadpollsquestions where thread=:th");
        Thread t=new Thread();
        t.setThreadId(Threadid);
	Iterator it2=qur2.setParameter("th",t ).list().iterator();
        Threadpollsquestions qeust2=null;
        if(it2.hasNext())
        qeust2=(Threadpollsquestions)it2.next();
        return qeust2;
    }




    //////Tested by Ahmed Mahmoud Abdelmohsen
    //public static void main(String[] args) {
        /////////////////Test AddPolls
        /*ThreadPollsUtility util=new ThreadPollsUtility();
        Member m=new Member();
        m.setMemberId(1);
        Threadpollsquestions q=new Threadpollsquestions();
        q.setQuestion("What do you like?");
        Thread th=new Thread();
        th.setThreadId(2);
        q.setThread(th);
        Vector<Threadpollschoices> choices=new Vector<Threadpollschoices>();
        Threadpollschoices ch1=new Threadpollschoices();
        ch1.setChoice("I love it");
        ch1.setNoofselectors(0);
        ch1.setThreadpollsquestions(q);
        Threadpollschoices ch2=new Threadpollschoices();
        ch2.setChoice("I hate it");
        ch2.setNoofselectors(0);
        ch2.setThreadpollsquestions(q);
        choices.add(ch1);
        choices.add(ch2);
        new ThreadPollsUtility().addThreadPolls(q,choices,m);*/

        /////////////////Test EditPolls
        /*SessionFactory fac=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
		Session session=fac.openSession();
		Query qur= session.createQuery("from  Threadpollsquestions ");
		Iterator it=qur.list().iterator();
                Threadpollsquestions qeust=(Threadpollsquestions)it.next();
                qeust.setQuestion("Hi");
                new ThreadPollsUtility().editThreadPollsQuestion(qeust);


                Vector<Threadpollschoices> vc=new ThreadPollsUtility().getThreadPollsChoices(1);
                Threadpollschoices c=vc.get(0);
                c.setChoice("56");
                Threadpollschoices c2=vc.get(1);
                c2.setChoice("57");
                new ThreadPollsUtility().editThreadPollsQuestionsAndChoices(qeust,vc);*/
        /////////////Test Delete
       // ThreadPollsUtility util=new ThreadPollsUtility();
        /*Member m=new Member();
        m.setMemberId(1);
        util.deleteThreadPolls(10,m);*/
        ////////////Test Edit Single CHoice
       /* Vector<Threadpollschoices> vc=util.getThreadPollsChoices(18);
        Threadpollschoices c=vc.get(0);
        c.setNoofselectors(15);
        new ThreadpollschoicesHome().delete(c);*/
        //util.editThreadPollsChoice(c);
        //String s=new ThreadpollschoicesHome().findById(24).getChoice();
      //  System.out.print("*************"+s);
    //}



}
